<template>
	<view class="container">
		<view class="publish-box">
			<view class="content-box">
				<view class="title-box"><text style="color: red">*</text> 问题和意见：
				</view>
				<view>
					<u--textarea v-model="description"></u--textarea>
				</view>
				<view class="title-box"><text style="color: red">*</text> 手机号：
				</view>
				<view>
					<u--input v-model="phoneNumber"></u--input>
				</view>
			</view>
			<view class="content-box">
				<view class="title-box"><text style="color: red">*</text> 反馈图片：
				</view>
				<view>
					<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" multiple :maxCount="10"
						:previewFullImage="true"></u-upload>
				</view>
			</view>
		</view>
		<view style="
        padding: 30rpx 100rpx;
        position: fixed;
        bottom: 50rpx;
        width: 100%;
        box-sizing: border-box;
      ">
			<u-button type="primary" color="#216be6" text="提交" @click="publishClick"></u-button>
		</view>
	</view>
</template>

<script>
	import baseUrl from "@/api/axios/baseUrl.js";
	import store from "@/store/index.js";
	import {
		feedback
	} from "@/api/axios/center.js";
	export default {
		data() {
			return {
				phoneNumber: "",
				description: "",
				fileList: [],
				openId: "",
				mapData: {},
				id: "",
			};
		},
		methods: {
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1);
			},
			// 新增图片
			async afterRead(event) {
				console.log(event);
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file);
				console.log(this[`fileList${event.name}`]);
				let fileListLen = this[`fileList${event.name}`].length;
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url);
					let item = this[`fileList${event.name}`][fileListLen];
					this[`fileList${event.name}`].splice(
						fileListLen,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					fileListLen++;
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					uni.uploadFile({
						url: baseUrl.url + "/xihefile/user/file/upload",
						filePath: url,
						name: "file",
						formData: {},
						success: (uploadFileRes) => {
							console.log(uploadFileRes);
							console.log(uploadFileRes.data.data);
							let url = JSON.parse(uploadFileRes.data).data;
							console.log(url, "url");
							resolve(url);
						},
					});
				});
			},
			publishClick() {
				let openId = uni.getStorageSync("openId");
				this.openId = openId;
				let url = this.fileList
					.map((item) => {
						return item.url;
					})
					.join(",");
				if (!this.description) {
					uni.showToast({
						icon: "error",
						title: "请输入反馈内容",
						duration: 2000,
					});
					return false;
				} else if (!this.phoneNumber) {
					uni.showToast({
						icon: "error",
						title: "请输入手机号",
						duration: 2000,
					});
					return false;
				} else if (!this.fileList.length) {
					uni.showToast({
						icon: "error",
						title: "请上传反馈图片",
						duration: 2000,
					});
					return false;
				}
				console.log(this.fileList.length);
				feedback({
					openId: openId,
					image: this.fileList[0].url,
					description: this.description,
					phoneNumber: this.phoneNumber,
				}).then(
					(res) => {
						uni.showToast({
							icon: "none",
							title: "反馈成功！",
							duration: 2000,
							success() {
								setTimeout(() => {
									uni.navigateBack({
										delta: 1,
									});
								}, 2000);
							},
						});
					},
					(reject) => {}
				);
			},
		},
		created() {},
		mounted() {},
		onLoad(data) {
			let _this = this;
			this.id = data.id;
			let openId = uni.getStorageSync("openId");
		},
	};
</script>

<style lang="scss" scoped>
	@import "./index.scss";
</style>